package com.system.transfer.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.system.employee.dao.EmployeeRepository;
import com.system.employee.entity.Employee;
import com.system.transfer.dao.TransferRepository;
import com.system.transfer.entity.Transfer;
@Service
@Transactional
public class TransferService implements ITransferService {
	@Autowired
	private TransferRepository transferRepository;
	@Autowired
	private EmployeeRepository employeeRepository;
	
	public void saveOrUpdate(Transfer transfer) {
		Employee employee=employeeRepository.findOne(transfer.getEmployee().getEmpId());
		if(employee!=null) {
			transfer.setPrePosition(employee.getPost().getPostName());
			transfer.setPrePart(employee.getPost().getPart().getPartName());
			employee.setPost(transfer.getEmployee().getPost());
			employeeRepository.save(employee);
			transfer.setEmployee(employee);
		    transferRepository.save(transfer);
		}
	}
	public void delete(Transfer transfer) {
		
		transferRepository.delete(transfer);
	}
	public void delete(List<Long> ids) {
		List<Transfer> transfers=(List<Transfer>) transferRepository.findAll(ids);
		if(transfers!=null) {
		transferRepository.delete(transfers);
		}
	}
	public Transfer findOne(Long id) {
		return transferRepository.findOne(id);
		
	}
	public List<Transfer> findAll(){
		return (List<Transfer>) transferRepository.findAll();
	}

	public Page<Transfer> findPage(Specification<Transfer> spec,Pageable pageable) {
		
		return transferRepository.findAll(spec,pageable);
	}
	
	public List<Transfer> findAll(List<Long> ids) {
		
		return (List<Transfer>) transferRepository.findAll(ids);
	}

}
